#include <cstdlib>
#include <cstdio>
#include <ctime>

#include "config.h"
#include "network.h"
#include "nef.h"
#include "dump.h"

int main(int argc, char *argv[])
{
    //Check the argument count.
    if(argc < 2)
    {
        printf("No configuration file is found.\n");
        exit(-1);
    }
    //Record the start time.
    double time_start = clock();
    //Load the configuration file.
    printf("Loading configuration...");
    load_config(argv[1]);
    printf("Done\n");
    //Load the network.
    printf("Loading network framework...");
    load_vertex();
    load_edge();
    printf("Done\n");
    //Prepare the space first.
    printf("Allocate NE/NF calculation space...");
    ne_nf_prepare_space();
    printf("Done\n");
    //Calculate all the concentration NE and NF, then the Min and Max.
    printf("Calculating NE/NF...");
    ne_nf_calculate();
    printf("Done\n");
    printf("Calculating Max and Min NE/NF...");
    ne_nf_min_max_calculate();
    printf("Done\n");
    //Dump the NE and NF value.
    if(dump_ne_nf)
    {
        printf("Dump NE/NF data...");
        dump_csv_ne_nf();
        printf("Done\n");
    }
    //Convert to NEF.
    printf("Convert from NE/NF to NEF...");
    ne_nf_to_nef();
    printf("Done\n");
    //Dump the NEF value.
    if(dump_nef)
    {
        printf("Dump NEF data...");
        dump_csv_nef();
        printf("Done\n");
    }
    //Filter the EC data based on the configuration.
    printf("Filtering EC value...");
    filter_ec_from_nef();
    printf("Done\n");
    //Dump the EC data.
    if(dump_ec)
    {
        printf("Dump EC data...");
        dump_csv_ec();
        printf("Done\n");
    }
    //Now calculate for each axis.
    if(num_of_compounds > 1)
    {
        //Prepare the single compound axis.
        printf("Allocate axis NE/NF calculation space...");
        ne_nf_prepare_axis_space();
        printf("Done.\n");
        //Calculate NE/NF for all compounds.
        printf("Calculate single compound best NEF...");
        ne_nf_single_calculate();
        printf("Done\n");
        if(dump_single_ne_nf)
        {
            printf("Dump single compound NE/NF data...");
            dump_single_csv_ne_nf();
            printf("Done\n");
        }
        //Convert the single NE/NF to NEF
        printf("Convert from single compound NE/NF to NEF...");
        ne_nf_single_to_nef();
        printf("Done\n");
        if(dump_single_nef)
        {
            printf("Dump single compound NEF data...");
            dump_single_csv_nef();
            printf("Done\n");
        }
        //Filter the EC value, and get the median.
        printf("Filtering single compound EC value...");
        filter_ec_single_from_nef();
        printf("Done\n");
        if(dump_single_ec)
        {
            printf("Dump single compound EC data...");
            dump_single_csv_ec();
            printf("Done\n");
        }
    }
    //Calculate the Ki.
    //Mark the end time.
    double time_end = clock();
    //Print time usage.
    printf("time usage: %fms\n", time_end - time_start);
    return 0;
}
